//
//  MYXLuckyMoneyListViewModel.m
//  meiyinxiang
//
//  Created by apple on 2017/11/8.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXLuckyMoneyListViewModel.h"
#import "NearbyLuckyMoneyModel.h"
#import "MXYLuckymoneyViewModel.h"
#import "AppDelegate.h"
#import "MYXHomepageVocherModel.h"
#import "MYXHomepageVocherVIewModel.h"
@implementation MYXLuckyMoneyListViewModel
- (id)init{
   // [super init];
    if (self = [super init] ) {
    }
   
    return self;
}
- (void)loadLuckyMoneysWith5kmWithlon:(NSString *)lon lite:(NSString *)lite Completion:(finishBlock )finishBlock{
     
    if ([[MYXMemberMdel sharedInstance].userinfo.is_first isEqualToString:@"0"]) {//官方红包券是否被领取
        self.isReceivedVocher = NO;
    }else{
        self.isReceivedVocher = YES;
    }
    [self.viewModelList removeAllObjects];
    [self.vocherViewModelList removeAllObjects];
    NSString *userId = @"";
    NSString *sex = @"0";
    if ([MYY_User readUserFromFile].isLogin) {
        if ([MYY_User readUserFromFile].uid) {
            userId = [MYY_User readUserFromFile].uid;
        }
        if ([MYXMemberMdel sharedInstance].userinfo.sex ) {
            sex = [MYXMemberMdel sharedInstance].userinfo.sex;
        }
    }
    
    NSDictionary *param = @{
                            @"longitude":lon,
                            @"latitude":lite,
                            @"sex":sex,
                            @"id":userId
                            };
    [[Http sharedInstance] requestForMethod:GET Url:Get5kmLukyMoney_URL withParams:param completion:^(id data, NSError *error) {
        if (data) {
            int  nRet = [[data objectForKey:@"status"] intValue];
            NSString *message = data[@"message"];
            if (nRet == 1) {
                NSArray *moneyArray = data[@"data"][@"list1"];
                for (NSDictionary *dic in moneyArray) {
                    NSError *error = nil;
                    NearbyLuckyMoneyModel *luckyModel = [[NearbyLuckyMoneyModel alloc] initWithDictionary:dic error:&error];
                    MXYLuckymoneyViewModel *viewModel = [MXYLuckymoneyViewModel initWithNearbyLuckyMoneyModel:luckyModel];
                    [self.viewModelList addObject:viewModel];
                }
                NSArray *vocherArray = data[@"data"][@"list2"];
                for (NSDictionary *dic in vocherArray) {
                    NSError *error = nil;
                    MYXHomepageVocherModel *vocherModel = [[MYXHomepageVocherModel alloc] initWithDictionary:dic error:&error];
                    MYXHomepageVocherVIewModel *viewModel = [[MYXHomepageVocherVIewModel alloc] initWithVocherModel:vocherModel];
                    [self.vocherViewModelList addObject:viewModel];
                }
                
                finishBlock(YES,message);
            }
            
        }
        
        
    }];
    
    
}
- (void)getFirstTicketWithResult:(finishBlock)result{
    [[MYXHttpManager shared] getFirstTimeTicketWithResult:^(BOOL sucess, NSString *msg) {
        if (sucess) {
            result(YES,msg);
        }else{
            result(NO,msg);
        }
    }];
}
- (NSMutableArray *)viewModelList{
    if (_viewModelList == nil) {
        _viewModelList = [NSMutableArray array];
    }
    return _viewModelList;
}
- (NSMutableArray *)vocherViewModelList{
    if (_vocherViewModelList == nil) {
        _vocherViewModelList = [NSMutableArray array];
    }
    return _vocherViewModelList;
}
@end
